﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using Easy.BLL;
using Easy.Operation;
using Easy.Model;
using Newtonsoft.Json;
using Easy.Operation.Data;
using Newtonsoft.Json.Linq;
using Easy.Operation.Expression;
using System.Dynamic;



namespace Easy.Web.Service.Data
{
    /// <summary>
    /// GetMobileInitData 的摘要说明
    /// </summary>
    public class GetMobileInitData : BaseHttpHandler
    {

        public override void ProcessRequest(HttpContext context)
        {
            context.Response.ContentType = "text/plain";

            string entityName = context.Request["entityname"];  //实体名
            string order = context.Request["order"];    //排序字段名
            string filterData = context.Request["filter"];  //筛选器
            string ViewName = context.Request["viewname"];  //视图名
            if (string.IsNullOrWhiteSpace(ViewName)) ViewName = "AppDefaultView";
            int pageSize = 10;  //获取记录条数
            if (!string.IsNullOrWhiteSpace(context.Request["pagesize"]))
                pageSize = int.Parse(context.Request["pagesize"].ToString());
            int pageNum = 0;
            if (context.Request["pagenum"] != null)
            {
                pageNum = int.Parse(context.Request["pagenum"].ToString());

            }
            Guid lastid = Guid.Empty;    //临界点GUID
            if (!string.IsNullOrWhiteSpace(context.Request["lastid"]))
                lastid = new Guid(context.Request["lastid"]);
            string lastordervalue = context.Request["lastordervalue"]; //临界点排序字段值

            PageView pv = new PageView(entityName, this.GetSystemUser(), ViewName);
            DataService service = new DataService(entityName, this.CurrentUserId);
           

            #region 获取排序
            string orderworld = "";
            string ordersort = "asc";
            if (order == null || order == "" || order.ToLower() == "null")
            {
                Dictionary<string,Field> Fields = pv.Fields;

                //先循环得到默认排序字段
                foreach (KeyValuePair<string, Field> kv in Fields)
                {
                    if (!string.IsNullOrWhiteSpace(kv.Value.ViewOrderBy))
                    {
                        orderworld = kv.Value.Name;
                        ordersort = kv.Value.ViewOrderBy;
                        break;
                    }
                }
                //无默认排序字段，则将主字段作为排序
                if (orderworld == "")
                {
                    foreach (KeyValuePair<string, Field> kv in Fields)
                    {
                        if (kv.Value.IsPrimaryAtt.Value)
                        {
                            orderworld = kv.Value.Name;
                            ordersort = "asc";
                            break;
                        }
                    }
                }

                //无默认排序且无主字段，按选择列的第一个排序
                if (orderworld == "")
                {
                    foreach (KeyValuePair<string, Field> kv in Fields)
                    {
                        orderworld = kv.Value.Name;
                        ordersort = "asc";
                        break;
                    }
                }

                order = orderworld + " " + ordersort;
            }
            else
            {
                string[] s = order.Split(' ');
                orderworld = s[0];
                if (s.Length > 1) ordersort = s[1];
                
            }
            #endregion
            //获取Template
            ViewMapClass list = EntitySetting_Function.GetViewMapInfo(entityName, ViewName);
            
            JsHubBLL jh = new JsHubBLL();

            dynamic json = new ExpandoObject();

            json.Privilege = pv.UserPrivilege;
            json.Data= service.RetrieveNextPage_List(ViewName, filterData, order, pageSize, lastordervalue, lastid);
            json.Order = new { col = orderworld, type = ordersort };
            json.Templete= list.ViewTemplete;
            json.EntityField = pv.Fields;
            json.EntityOption = pv.Options;
            json.Views = pv.ViewsMO;
            json.JS = jh.GetJsWithEntityItemName("view", entityName, ViewName);
          
            context.Response.Write(JsonConvert.SerializeObject(json));


        }

    }
}